package cn.hd.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
//        从请求对象获取token
        String token = request.getHeader("token");

        //判断有没有token
        if(token == null || "null".equals(token) ){//没有登录，则拦截
            request.getRequestDispatcher("/tologin").forward(request,response);
            return false;
        }

        return true;
    }
}
